package com.liru.neo4j_demo.controller;

import com.liru.neo4j_demo.service.KnowledgeGraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 知识图谱控制器，处理HTTP请求
 */
@RestController
@RequestMapping("/knowledge-graph")
public class KnowledgeGraphController {

    private final KnowledgeGraphService knowledgeGraphService;

    @Autowired
    public KnowledgeGraphController(KnowledgeGraphService knowledgeGraphService) {
        this.knowledgeGraphService = knowledgeGraphService;
    }

    /**
     * 上传文档并构建知识图谱
     * @param file 上传的文件
     * @param knowledgeId 知识ID，用于数据隔离
     * @return 处理结果
     */
    @PostMapping("/upload")
    public ResponseEntity<?> uploadDocument(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "knowledgeId", defaultValue = "1") String knowledgeId) {
        try {
            Map<String, Object> result = knowledgeGraphService.processDocument(file, knowledgeId);
            return ResponseEntity.ok(result);
        } catch (IOException e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "文件处理失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
        } catch (Exception e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "处理请求失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
        }
    }

    /**
     * 向量检索
     * @param query 查询文本
     * @param knowledgeId 知识ID
     * @param limit 返回数量限制，默认为5
     * @return 检索结果
     */
    @GetMapping("/vector-search")
    public ResponseEntity<?> vectorSearch(
            @RequestParam("query") String query,
            @RequestParam("knowledgeId") String knowledgeId,
            @RequestParam(defaultValue = "5") int limit) {
        try {
            List<Map<String, Object>> results = knowledgeGraphService.vectorSearch(query, knowledgeId, limit);
            return ResponseEntity.ok(results);
        } catch (Exception e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "检索失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
        }
    }

    /**
     * 图扩展搜索
     * @param query 查询文本
     * @param knowledgeId 知识ID
     * @param limit 返回数量限制，默认为5
     * @return 搜索结果
     */
    @GetMapping("/graph-search")
    public ResponseEntity<?> graphExtendedSearch(
            @RequestParam("query") String query,
            @RequestParam("knowledgeId") String knowledgeId,
            @RequestParam(defaultValue = "5") int limit) {
        try {
            List<Map<String, Object>> results = knowledgeGraphService.graphExtendedSearch(query, knowledgeId, limit);
            return ResponseEntity.ok(results);
        } catch (Exception e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "搜索失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
        }
    }

    /**
     * 图搜索问答
     * @param query 查询问题
     * @param knowledgeId 知识ID
     * @param limit 相关片段数量限制，默认为5
     * @return 问答结果
     */
    @GetMapping("/graph-query")
    public ResponseEntity<?> graphQuery(
            @RequestParam("query") String query,
            @RequestParam("knowledgeId") String knowledgeId,
            @RequestParam(defaultValue = "5") int limit) {
        try {
            String result = knowledgeGraphService.queryKnowledgeGraph(query, knowledgeId, limit);
            Map<String, Object> response = new HashMap<>();
            response.put("answer", result);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "问答失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
        }
    }
}